module key_board (
	input sys_clk, 
	input sys_rst,
	input [3:0] key,
	output reg [3:0] output_key_id);
	
reg [31:0] key_debonse_timer;


parameter  KEY_ID_IDLE = 3'd0;
parameter  KEY_ID_KEY1 = 3'd0;
parameter  KEY_ID_KEY2 = 3'd0;
parameter  KEY_ID_KEY3 = 3'd0;
parameter  KEY_ID_KEY4 = 3'd0;
parameter  KEY_DEBONSE_PERIOD	= 32'd10000000;					//10,000,000/50M = 0.2s



always @(posedge sys_clk or negedge sys_rst)						begin 
	if (!sys_rst) 															begin 		//复位
		key_debonse_timer <= 1'b0;
		output_key_id <= KEY_ID_IDLE;
	end
	else 																		begin
		if (key != 4'b1111)												begin 		//有按键按下
			if (key_debonse_timer < (KEY_DEBONSE_PERIOD - 1'b1))	begin
				key_debonse_timer <= key_debonse_timer + 1'b1;
			end
			else																begin			//10m/50m = 0.2ms延时去抖
				key_debonse_timer <= 32'd0;
				case (key)
					4'b1110 : output_key_id = KEY_ID_KEY1;
					4'b1101 : output_key_id = KEY_ID_KEY2;
					4'b1011 : output_key_id = KEY_ID_KEY3;
					4'b0111 : output_key_id = KEY_ID_KEY4;
					default : output_key_id = KEY_ID_IDLE;
				endcase	
			end
		end
		else																		begin 	//无按键按下
			key_debonse_timer <= 32'd0;
			output_key_id <= KEY_ID_IDLE;
		end
	end
 
end

endmodule